#include<bits/stdc++.h>

using namespace std;
using ll = long long;
//#define endl '\n'
int a[20], book[20], n = 8;


void dfs(int k) {
    if (k >= n) {
        for (int i = 0; i < n; i++) {
            cout << a[i];
        }
        cout << endl;
        return;

    }
    for (int i = 0; i < n; i++) {
        if (book[i] == 1)continue;//查表
        //希望把Qk放在(k,i) ，保证Qk,与Q0.....Q[k-1] 不冲突
        bool flag = true;
        for (int j = 0; j <= k - 1; j++) {
            if (abs(i - a[j]) == (k - j)) {
                flag = false;
                break;
            }
        }

        if (!flag)continue;
        a[k] = i;

        book[i] = 1;//写入标记数组
        dfs(k + 1);
        book[i] = 0;//当我们访问完叶子结点后，必须要重置book[i]为未选择状态
    }


}


int main() {

    dfs(0);

    return 0;
}